package io.renren.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.renren.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局系统异常处理
 *
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 非法参数异常
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Result handleIllegalArgumentException(IllegalArgumentException e) {
        return Result.failed(e.getMessage());
    }


    /**
     * Json转换异常
     * @param e
     * @return
     */
    @ExceptionHandler(JsonProcessingException.class)
    public Result handleJsonProcessingException(JsonProcessingException e) {
        return Result.failed(e.getMessage());
    }

    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public Result handleBizException(CustomException e) {
        if (e.getResultCode() != null) {
            return Result.failed(e.getResultCode());
        }
        return Result.failed(e.getMessage());
    }

}
